<?php
class UniqueAttributesValidator extends CValidator 
{
    /**
     * The attributes boud in the unique contstraint with attribute
     * @var string
     */
    public $with;

    /**
     * Validates the attribute of the object.
     * If there is any error, the error message is added to the object.
     * @param CModel $object the object being validated
     * @param string $attribute the attribute being validated
     */
    protected function validateAttribute($object,$attribute) {
        $with = is_array($this->with) ? $this->with : array_map('trim', explode(",", $this->with)) ;
        if (count($with) < 1)
            throw new Exception("Attribute 'with' not set");

        $uniqueValidator = new CUniqueValidator();
        $uniqueValidator->attributes = array($attribute);
        $uniqueValidator->message = $this->message;
        $uniqueValidator->on = $this->on;
        $conditionParams = array();
        $params = array();
        foreach ($with as $attribute) 
        {
            $fieldName = Yii::app()->getDb()->quoteColumnName($attribute);
            $paramName = ":{$attribute}";
            $conditionParams[] = "$fieldName=$paramName";
            $params[$paramName] = $object->$attribute;
        }
        $condition = implode(' AND ', $conditionParams);
        $uniqueValidator->criteria = array(
            'condition'=>$condition,
            'params'=>$params
        );
        $uniqueValidator->validate($object);
    }
}